const app = getApp();
Page({
    data: {
      key:'',
      products:[],
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        TabbarBot: app.globalData.tabbar_bottom,
        TabCur: 0,scrollLeft:0,
        SortMenu: [{id:0,name:"商品列表"},{id:1,name:"店铺列表"}]
    },
    onLoad: function (options) {
        let that = this;
        this.setData({
          key:options.key
        })
        this.search()
    },
    tabSelect(e) {
        console.log(e.currentTarget.dataset.id);
        this.setData({
            TabCur: e.currentTarget.dataset.id,
            scrollLeft: (e.currentTarget.dataset.id-1)*60
        })
        this.search(e)
    },
    getKey:function(e){
      this.setData({
        key: e.detail.value.key
      })
      this.search()
    },
    search:function(){
      if (this.data.TabCur == 0) {
        wx:wx.request({
          url: 'http://localhost:8989/product/key/' + this.data.key,
          method: "GET",
          success: (result) => {
            console.log(result.data.data)
            this.setData({
              products: result.data.data
            })
          },
          fail: (err) => {},
          complete: (res) => {},
        })
      }else if (this.data.TabCur == 1) {
        wx:wx.request({
          url: 'http://localhost:8989/shop/key/' + this.data.key,
          method: "GET",
          success: (result) => {
            let list = []
            for(var i = 0; i < result.data.data.length; i ++){
              let ob = new Object
              ob.id = result.data.data[i].id
              ob.title = result.data.data[i].shopName
              ob.image = result.data.data[i].image
              ob.price = result.data.data[i].shopMessage
              list.push(ob)
            }
            console.log(list)
            this.setData({
              products: list
            })
          },
          fail: (err) => {},
          complete: (res) => {},
        })
      }

    }
});
